package net.hyperz.water.levels.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.Map;

import org.jsoup.connect.Connection;
import org.jsoup.connect.Connection.Method;
import org.jsoup.connect.Connection.Response;
import org.jsoup.helper.HttpConnection;
import org.jsoup.nodes.Document;

import android.graphics.Bitmap;

public abstract class HttpLoad {
	private final static String PROXY_HOST = "cavtsase162.corp.advancedsolutions.eds.com";
	private final static int PROXY_PORT = 8080;
	private final static String PROXY_UID = "siegleda";
	private final static String PROXY_PWD = "Calgary99!1";

	// public static boolean useProxy = true;
	public static boolean useProxy = false;

	public static Response postPage(String url, Map<String, String> cookies,
			Map<String, String> postData) throws IOException {
		try {
			Connection jsConn = HttpConnection.connect(url);

			if (useProxy)
				((HttpConnection) jsConn).proxy(PROXY_HOST, PROXY_PORT,
						PROXY_UID, PROXY_PWD);

			jsConn.timeout(12000).userAgent("Mozilla").cookies(cookies)
					.data(postData).method(Method.POST);

			return jsConn.execute();
		} catch (IOException e) {
			throw e;
		}
	}

	public static Document postPage(String url, Map<String, String> cookies,
			String... keyvals) throws IOException {
		try {
			Connection jsConn = HttpConnection.connect(url);

			if (useProxy)
				((HttpConnection) jsConn).proxy(PROXY_HOST, PROXY_PORT,
						PROXY_UID, PROXY_PWD);

			jsConn.timeout(12000).userAgent("Mozilla").cookies(cookies)
					.data(keyvals).method(Method.POST);

			return jsConn.post();
		} catch (IOException e) {
			throw e;
		}
	}

	public static Document getDocument(String url, Map<String, String> cookies,
			String... keyvals) throws IOException {
		try {

			Connection jsConn = HttpConnection.connect(url);

			if (useProxy)
				((HttpConnection) jsConn).proxy(PROXY_HOST, PROXY_PORT,
						PROXY_UID, PROXY_PWD);

			jsConn.timeout(6000);
			jsConn.userAgent("Mozilla");
			jsConn.data(keyvals);
			jsConn.cookies(cookies);

			return jsConn.get();
		} catch (IOException e) {
			throw e;
		}
	}

	public static Document getDocument(String url, String... keyvals)
			throws IOException {
		try {

			Connection jsConn = HttpConnection.connect(url);

			if (useProxy)
				((HttpConnection) jsConn).proxy(PROXY_HOST, PROXY_PORT,
						PROXY_UID, PROXY_PWD);

			jsConn.timeout(6000);
			jsConn.userAgent("Mozilla");
			jsConn.data(keyvals);

			return jsConn.get();
		} catch (IOException e) {
			throw e;
		}
	}

	public static byte[] LoadImage(String URL)

	{

		byte[] buffer = new byte[0x20000];
		ByteBuffer byteData = null;
		
		URL url;
		
		try {
			url = new URL(URL);
			
			HttpURLConnection conn = (HttpURLConnection) url
					.openConnection();
			
			InputStream in = conn.getInputStream();
			ByteArrayOutputStream outStream = new ByteArrayOutputStream(0x20000);

			int read;
			while (true) {
				read = in.read(buffer);
				if (read == -1)
					break;
				outStream.write(buffer, 0, read);
			}

			byteData = ByteBuffer.wrap(outStream.toByteArray());
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return byteData.array();

	}
	
	public static byte[] ConvertImage(Bitmap bitmap) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);

		return baos.toByteArray();

	}
}
